package com.tuanda.app.customviewdemo.paint;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;

import com.tuanda.app.customviewdemo.BaseCustomView;
import com.tuanda.app.customviewdemo.R;

/**
 * Created by LiDongduo on 2018/9/21.
 */
public class PaintXfermodeView extends BaseCustomView {
    public PaintXfermodeView(Context context) {
        super(context);
    }

    public PaintXfermodeView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PaintXfermodeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_batman_logo);
        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

        Xfermode xfermode1 = new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP);
        Xfermode xfermode2 = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);
        Xfermode xfermode3 = new PorterDuffXfermode(PorterDuff.Mode.DST_OUT);

        int saved = canvas.saveLayer(null,null,Canvas.ALL_SAVE_FLAG);

        canvas.drawBitmap(bitmap1,0,0,mPaint);
        mPaint.setXfermode(xfermode1);
        canvas.drawBitmap(bitmap2,0,0,mPaint);
        mPaint.setXfermode(null);

        canvas.drawBitmap(bitmap1,bitmap1.getWidth() + 10,0,mPaint);
        mPaint.setXfermode(xfermode2);
        canvas.drawBitmap(bitmap2,bitmap1.getWidth() + 10,0,mPaint);
        mPaint.setXfermode(null);

        canvas.drawBitmap(bitmap1,0,bitmap1.getHeight() + 10,mPaint);
        mPaint.setXfermode(xfermode2);
        canvas.drawBitmap(bitmap2,0,bitmap1.getHeight() + 10,mPaint);
        mPaint.setXfermode(null);

        canvas.restoreToCount(saved);

    }
}
